{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Getting Started Wih Python\n",
    "To get deeper knowledge of how things are working in Python. Below are the few basic commands to start off with the python.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Getting Help\n",
    "Help in Python is always available right in the interpreter. You can use help(object) to know how functions work. The dir() shows all the object's methods for e.g dir(abs). Lastly object.__doc__, which shows the documention for the function which is also acessible by help(object)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function abs in module builtins:\n",
      "\n",
      "abs(x, /)\n",
      "    Return the absolute value of the argument.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Return the absolute value of the argument.'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# To get help of any function\n",
    "help(abs)\n",
    "\n",
    "#\n",
    "dir(abs)\n",
    "\n",
    "#\n",
    "abs.__doc__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Syntax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myvar: 5\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "myvar = 3\n",
    "myvar += 2\n",
    "print(\"myvar:\", myvar) # Print value of myvar\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Array and Lists\n",
    "List are 1-D arrays, dictionaries are associative arrays and tuples are immutable 1-D arrays. Pythons array can be of any type and you can also mix several datatypes to construct one array e.g. integers, strings etc. \n",
    "Array can be access in two ways: With +ve indexing starting with \"0\" or with -ve indexing starting from end towards the beginning where \"-1\" represent the last item. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sample = [1, [\"another\" , \"list\"] , (\"a\" , \"tupple\")] \n",
    "mylist = [\"List item 1\" , 2,3,4]\n",
    "mylist[0] = \"List item change\"\n",
    "mylist[-1] = 3.21\n",
    "mydict = {\"Key1\" : \"Value 1\" , 2: 3 , \"pi\" : 3.14}\n",
    "mydict[\"pi\"]\n",
    "mytuple = (1,2,3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['List item 1', 2, 3.14]\n",
      "['List item 1', 2]\n",
      "List item 1\n",
      "3.14\n",
      "['List item 1', 2]\n",
      "[]\n",
      "[2, 3.14]\n",
      "['List item 1', 3.14]\n"
     ]
    }
   ],
   "source": [
    "# More ways of accessing arrays \n",
    "myList = [\"List item 1\", 2 , 3.14] \n",
    "# 0 , 1 , 2 \n",
    "# -3, -2 , -1 \n",
    "print (myList[:])\n",
    "print (myList[:2])\n",
    "print (myList[0])\n",
    "print (myList[-1])\n",
    "print (myList[-3:-1])\n",
    "print (myList[:-3])\n",
    "print (myList[1:])\n",
    "\n",
    "## Accessing 1st and element on index 2 \n",
    "print (myList[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions\n",
    "Function can be access in many ways in Python. One of the most usefull way is to assign function to a variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "mylist = [\"List item 1\" , 2,3,4,5,6 ,7 ]\n",
    "myfunction = len\n",
    "print (myfunction(mylist))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings \n",
    "For any language parsing string is very important and Python provide very rich way of accessing Strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: aman Number: 3 String: ---\n",
      "This is\n",
      "a multiline\n",
      "string.\n",
      "This is a test\n"
     ]
    }
   ],
   "source": [
    "print (\"Name: %s Number: %s String: %s\" % (\"aman\", 3 ,3 * \"-\"))\n",
    "\n",
    "\n",
    "strString = \"\"\"This is\n",
    "a multiline\n",
    "string.\"\"\"\n",
    "\n",
    "print (strString)\n",
    "\n",
    "# WARNING: Watch out for the trailing s in \"%(key)s\".\n",
    "\n",
    "print(\"This %(verb)s a %(noun)s\" % {\"verb\" : \"is\", \"noun\" : \"test\"})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "Oops, invalid.\n",
      "We are done with that.\n"
     ]
    }
   ],
   "source": [
    "class MyClass(object):\n",
    "    common = 10\n",
    "    def __init__(self):\n",
    "        self.myvariable = 3\n",
    "    def myfunction(self, arg1,arg2):\n",
    "        return self.myvariable\n",
    "\n",
    "# This is the class instantiation\n",
    "classinstance = MyClass()\n",
    "classinstance.myfunction(1,2)\n",
    "\n",
    "classinstance.common = 30\n",
    "print (classinstance.common)\n",
    "\n",
    "def someFunction():\n",
    "    try:\n",
    "        10/0\n",
    "    except ZeroDivisionError:\n",
    "        print (\"Oops, invalid.\")\n",
    "    else:\n",
    "        # Exception didn't occur\n",
    "        pass\n",
    "    finally:\n",
    "        print(\"We are done with that.\")\n",
    "\n",
    "someFunction()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Flow Contrrol Statements "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References\n",
    "1) https://www.stavros.io/tutorials/python/"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
